package com.linmo.personalcenter;

import com.linmo.MainJFrameGUI;
import com.linmo.loginmanage.LoginJFrameGUI;
import com.linmo.loginmanage.User;
import com.linmo.loginmanage.UserManage;
import com.linmo.questionmatcher.QuestionMatcher;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PersonalPanelGUI extends JPanel {
    private JLabel usernameLabel;
    private JButton logoutButton;
    private JButton deleteUserButton;
    private JButton changePasswordButton;
    private JButton updateDialogueLibraryButton;
    private MainJFrameGUI mainFrame;
    private static UserManage userManage = new UserManage();
    private QuestionMatcher questionMatcher ;

    public PersonalPanelGUI(User user, MainJFrameGUI mainFrame) {
        this.mainFrame = mainFrame;
        this.questionMatcher = mainFrame.getQuestionMatcher();

        setLayout(new GridBagLayout());
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 创建圆形框
        JPanel circlePanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(Color.gray);
                int diameter = 80; // 自定义直径
                int x = (getWidth() - diameter) / 2;
                int y = (getHeight() - diameter) / 2;
                g2d.fillOval(x, y, diameter, diameter);

                g2d.setColor(Color.WHITE);
                int fontSize = 20;
                g2d.setFont(new Font("Arial", Font.BOLD, fontSize));
                FontMetrics fm = g2d.getFontMetrics();
                String text = user.getName();
                int textWidth = fm.stringWidth(text);
                int textHeight = fm.getAscent();
                g2d.drawString(text, (getWidth() - textWidth) / 2, (getHeight() + textHeight) / 2 - 5);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(80, 80);
            }
        };

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
        buttonPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
        //buttonPanel.setBackground(Color.BLUE);

        logoutButton = new JButton("退出登录");
        deleteUserButton = new JButton("注销用户");
        changePasswordButton = new JButton("修改密码");
        updateDialogueLibraryButton = new JButton("更新对话库");

        buttonPanel.add(logoutButton);
        buttonPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        buttonPanel.add(deleteUserButton);
        buttonPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        buttonPanel.add(changePasswordButton);
        buttonPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        buttonPanel.add(updateDialogueLibraryButton);

        // 添加动作监听器
        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 处理退出登录逻辑
                int result = JOptionPane.showConfirmDialog(PersonalPanelGUI.this, "确定要退出登录吗？", "退出登录", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    // 关闭当前窗口并返回到登录界面
                    mainFrame.dispose();
                    // 这里可以添加返回到登录界面的逻辑
                    new LoginJFrameGUI().setVisible(true);
                }
            }
        });

        deleteUserButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 处理注销用户逻辑
                int result = JOptionPane.showConfirmDialog(PersonalPanelGUI.this, "确定要注销用户吗？", "注销用户", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    // 调用用户管理模块的注销用户方法
                    UserManage.deleteUser(user.getId());
                    JOptionPane.showMessageDialog(PersonalPanelGUI.this, "用户已注销");
                    // 关闭当前窗口并返回到登录界面
                    mainFrame.dispose();
                    // 这里可以添加返回到登录界面的逻辑
                    new LoginJFrameGUI().setVisible(true);
                }
            }
        });

        changePasswordButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 处理修改密码逻辑
                ChangePasswordDialog changePasswordDialog = new ChangePasswordDialog(user.getId(), mainFrame);
                changePasswordDialog.setVisible(true);
            }
        });

        updateDialogueLibraryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 处理修改对话语库逻辑
                EditDialogueDialog editDialogueDialog = new EditDialogueDialog(mainFrame, questionMatcher);
                editDialogueDialog.setVisible(true);
            }
        });

        // 使用 GridBagConstraints 配置布局
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(0, 0, 20, 0); // 设置底部间距
        add(circlePanel, gbc);

        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(0, 0, 0, 0); // 重置间距
        add(buttonPanel, gbc);
    }
}
